/**
 * @author liuxin
 * @e-mail lxtobe5@gmail.com
 * @update 2011.11.19
 */
package API_sample.TopLevel_samples.GlobalFunctions_samples 
{
	/**
	 *Array方法属性实例 
	 */	
	import com.lx.utils.ArrayUtil;
	import flash.display.Sprite;
	
	public class Array_sample extends Sprite
	{
		private var arr1:Array;
		private var arr2:Array;
		private var arr3:Array;
		private var arr4:Array;
		private var a1:Array;
		private var a2:Array;
		private var a3:Array;
		private var a4:Array;
		
		public function Array_sample()
		{
			arr1 = ArrayUtil.weekArray();
			a1 = arr1.concat();
			arr2 = ArrayUtil.NumberArray(1,10);
			a2 = arr2.concat();
			arr3 = ArrayUtil.NumberStringArray(1,10);
			a3 = arr3.concat();
			arr4 = ArrayUtil.charNumberArray("k",1,10);
			a4 = arr4.concat();
			
			switch(4)
			{
				case 0:
					break;
				case 1:
					//indexOf(searchElement:*, fromIndex:int = 0):int
					indexOf_example();
					break;
				case 2:
					//lastIndexOf(searchElement:*, fromIndex:int = 0x7fffffff):int
					lastIndexOf_example();
					break;
				case 3:
					//Array.slice(startIndex:int = 0, endIndex:int = 16777215):Array
					slice_example(3,7);
					break;
				case 4:
					//splice(startIndex:int, deleteCount:uint, ... values):Array
					splice_example(3,16,a3);
					break;
				case 5:
					//concat(... args):Array
					concat_example(a1,a2,a3,a4);
					break;
				case 6:
					//join(sep:*):String
					join_example(a1,"|");
					break;
				case 7:
					//reverse():Array
					reverse_example(a4);
					break;
				case 8:
					//sort(... args):Array
					sort_example(a2);
					break;
				case 9:
					//sortOn(fieldName:Object, options:Object = null):Array
					sortOn_example(a4);
					break;
			}
			
			switch(0)
			{
				case 0:
					break;
				case 1:
					special_example1();
					break;
				case 2:
					special_example2();
					break;
				case 3:
					special_example3();  //包含every forEach filter map some
					break;
			}
		}
		
		private function indexOf_example():void
		{
			trace("按索引从小开始查sunday："+arr1.indexOf("sunday"));
			trace("按索引从小开始查Sunday："+arr1.indexOf("Sunday"));
			trace("按索引从小开始查Monday："+arr1.indexOf("Monday"));
			trace("从第三个开始按索引从小开始查Sunday："+arr1.indexOf("Sunday",3));
		}
		
		private function lastIndexOf_example():void
		{
			trace("按索引从大开始查sunday："+arr1.lastIndexOf("sunday"));
			trace("按索引从大开始查Sunday："+arr1.lastIndexOf("Sunday"));
			trace("按索引从大开始查Monday："+arr1.lastIndexOf("Monday"));
			trace("从第三个开始按索引从小开始查Sunday："+arr1.lastIndexOf("Sunday",3));
		}
		
		private function slice_example(startIndex:int = 0, endIndex:int = 16777215):void
		{
			trace(a3.slice(startIndex,endIndex));
		}
		
		private function splice_example(index:int,delNum:int,argument:*):void
		{
			trace("删除的元素："+a4.splice(index,delNum,argument));
			trace("原数组并新增："+a4);
		}
		
		private function concat_example(arg1:*,arg2:*,...args):void
		{
			trace("array.concat(...args):"+arg1.concat(arg2,args));
		}
		
		private function join_example(arg:Array,str:String):void
		{
			trace("arg.join("+str+"):"+arg.join(str));
		}
		
		private function reverse_example(array:Array):void
		{
			trace("array的正序："+array);
			trace("array的倒序："+array.reverse());
		}
		
		private function sort_example(array:Array):void
		{
			trace("array:"+array);
			trace("array.sort(Array.CASEINSENSITIVE):"+array.sort(Array.CASEINSENSITIVE));
			trace("array.sort(Array.DESCENDING):"+array.sort(Array.DESCENDING));
			trace("array.sort(Array.UNIQUESORT):"+array.sort(Array.UNIQUESORT));
			trace("array.sort(Array.RETURNINDEXEDARRAY):"+array.sort(Array.RETURNINDEXEDARRAY));
			trace("array.sort(Array.NUMERIC):"+array.sort(Array.NUMERIC));
		}
		
		private function sortOn_example(array:Array):void
		{
			trace("array:"+array);
			trace("array.sortOn(\"name\"):"+array.sortOn("name"));
		}
		
		private function special_example1():void
		{
			trace("arr2[1]==arr3[1]:"+(arr2[1]==arr3[1]));
			trace("arr2[1]===arr3[1]:"+(arr2[1]===arr3[1]));
			trace("arr2.indexOf(a3[1]):"+arr2.indexOf(arr3[1]));
			trace("使用'==='不会执行类型转换");
		}
		
		private function special_example2():void
		{
			var obj:Object = {name:"Objectname",type:"Objecttype"};
			var arr:Array = a1.concat();
			var arr_copy:Array =arr.slice();
			trace("arr==arr_copy:"+(arr==arr_copy)+"==判断变量持有对象引用是否相同，不是判断内容");
		}
		
		private function special_example3():void
		{
			var o1:Object = {name:"flash",author:"adobe"};
			var o2:Object = {name:"flex",author:"adobe"};
			var o3:Object = {name:"dreamweaver",author:"adobe"};
			var o4:Object = {name:"facebook",author:"mark"};
			var o5:Object = {name:"wow",author:"bazz"};
			
			var array:Array = [o1,o2,o3,o4,o5];
			var value:Array = [100,200,300,400,500];
			
			if(!array.every(every_method))
			{
				trace("不能打开");
			}
			//every对数组中的每一项执行测试函数，直到获得对指定的函数返回 false 的项。
			//every用法：如果发现facebook，就表示页面打不开
			function every_method(element:*,index:int,arr:Array):Boolean
			{
				if(element.name.indexOf("facebook")!=-1)
				{
					trace("找到"+element.name);
					return false;
				}
				return true;
			}
			
			var filterArray:Array = array.filter(filter_method);
			//filter对数组中的每一项执行测试函数，并构造一个新数组，其中的所有项都对指定的函数返回 true。如果某项返回 false，则新数组中将不包含此项。
			//filter用法：将作者adobe做成一个新的数组
			function filter_method(element:*,index:int,arr:Array):Boolean
			{
				if(element.author=="adobe")
					return true;
				return false;
			}
			trace("filterArray.length:"+filterArray.length);
			trace("filterArray[0].author:"+filterArray[0].author);
			
			//forEach对数组中的每一项执行函数。
			//forEach用法：给每个name的值都加上
			array.forEach(forEach_method);
			function forEach_method(element:*,index:int,arr:Array):void
			{
				element.name += "soft";
			}
			for each (var i:* in array)
			{
				trace("name:"+i.name+"  author:"+i.author);
			}
			
			//map对数组中的每一项执行函数并构造一个新数组，其中包含与原始数组中的每一项的函数结果相对应的项。
			//map用法：生成一个新的数组，加入value数组
			var mapArray:Array = array.map(map_method);
			function map_method(element:*,index:int,arr:Array):Object
			{
				var oo:Object = {};
				oo.name = element.name;
				oo.author = element.author;
				oo.value = value[index];
				return oo;
			}
			for each (var m:* in mapArray)
			{
				trace("name:"+m.name+"  author:"+m.author+"  value:"+m.value);
			}
			
			//对数组中的每一项执行测试函数，直到获得返回 true 的项。使用此方法确定数组中的所有项是否满足条件，如具有小于某一特定数值的值。
			//some用法：如果发现mark，就标识
			trace("这里有mark吗？"+array.some(some_method));
			function some_method(element:*,index:int,arr:Array):Boolean
			{
				if(element.author=="mark")
					return true;
				return false;
			}
		}
	}
}